package com.openmind.pichosting.controller;

import com.openmind.pichostring.vo.RespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhoujunwen
 * @date 2019-09-11 10:45:52
 */
@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {

    @RequestMapping(value = "single", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public RespVo upload(@RequestParam("file") MultipartFile file) {
        long fileSize = file.getSize();
        String originFileName = file.getOriginalFilename();
        String mineType = file.getContentType();
        String name = file.getName();

        String temp = "/tmp/" + originFileName;
        try {
            file.transferTo(new File(temp));
        } catch (Exception e) {

        }
        log.info("fileSize:{}, originName:{}, name:{}, content-type:{}", fileSize, originFileName, name, mineType);
        Map<String, String> data = new HashMap<>();
        data.put("url", "https://segmentfault.com/img/remote/1460000016082608");
        data.put("thumbUrl", "https://upload-images.jianshu.io/upload_images/3012005-2eecd990df12386f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300");
        data.put("imgId", "1460000016082608");
        return RespVo.ok(data);
    }

    @PostMapping(value = "paste", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public RespVo upload(@RequestParam("content") String content) {
        System.out.println(content);
//        log.info("fileSize:{}, originName:{}, name:{}, content-type:{}", fileSize, originFileName, name, mineType);
        Map<String, String> data = new HashMap<>();
        data.put("url", "https://segmentfault.com/img/remote/1460000016082608");
        data.put("thumbUrl", "https://upload-images.jianshu.io/upload_images/3012005-2eecd990df12386f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300");
        data.put("imgId", "1460000016082608");
        return RespVo.ok(data);
    }
}
